package org.example.forum.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Board;
import org.example.forum.service.IBoardService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    //如果在配置文件没有找到这个数值，设置一个默认值9
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;

    @ApiOperation("主页显示板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块数量为"+indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        //非空校验
        if (boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getByBoardId")
    public AppResult<Board> getBoardById(@NonNull Long id){
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
